package cn.com.generaldata.os.ws1;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.result.PacketRs;
import cn.com.generaldata.os.service.intf.ReportServiceInterface;
import cn.com.generaldata.os.ws.intf.ReportedDataWSInterface;
import cn.com.generaldata.os.ws1.util.ConfigProperties;
import cn.com.generaldata.os.ws1.util.DomUtil;

public class ModelSend implements Runnable, ApplicationContextAware {

	private static Logger log = LoggerFactory.getLogger(ModelSend.class);

	private static ApplicationContext ctx;
	private Object obj;
	private String flag;
	private BaseDAOInterface<User> userDAO;

	public BaseDAOInterface<User> getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(BaseDAOInterface<User> userDAO) {
		this.userDAO = userDAO;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	private Boolean sender(String xml, String serviceId) {
		String flag = ConfigProperties.getPropertyByName("flag");
		boolean result = false;
		if ("1".equals(flag)) {
			String url = ConfigProperties.getPropertyByName("urlAddress");
			URL wsdlURL;
			try {
				wsdlURL = new URL(url);
				QName UserServiceName = new QName(WsConstants.NS, "reportedDataWebService");
				Service service = Service.create(wsdlURL, UserServiceName);
				ReportedDataWSInterface ws = service.getPort(ReportedDataWSInterface.class);
				PacketRs rs = null;
				if ("12001".equals(serviceId) || "12002".equals(serviceId)) {
					rs = ws.BusinessInfor(xml);
				} else if ("12003".equals(serviceId) || "12004".equals(serviceId)) {
					rs = ws.ReportData(xml);
				} else if ("12005".equals(serviceId)) {
					rs = ws.synchronizationData(xml);
				}
				String mes = rs.getMessage();
				result = rs.isSuccess();

			} catch (MalformedURLException e) {
				log.error("调用国家海洋局webservice", e);
			}
		}
		return result;
	}

	public Boolean sendReport(Object entity, String serviceId) {
		String content = DomUtil.getOrgbw(entity, serviceId);
		// 替换报文内容
		String xml = WsConstants.REQUEST_MODEL.replace("@content", content);
		// 替换报文@serviceId
		xml = xml.replace("@serviceId", serviceId);
		// 替换报文@channelId
		xml = xml.replace("@channelId", WsConstants.SERVICE_CHANNELID);
		// System.out.println("456"+xml.toString());
		Boolean ret = sender(xml, serviceId);
		return ret;
	}

	// public ModelSend(Object obj, String flag){
	// this.obj = obj;
	// this.flag = flag;
	// }

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		ModelSend.ctx = arg0;

	}

	@Override
	public void run() {
		String flag = ConfigProperties.getPropertyByName("flag");
		if ("1".equals(flag)) {
			ReportServiceInterface reportService = (ReportServiceInterface) ctx.getBean("reportService");
			userDAO = (BaseDAOInterface<User>) ctx.getBean("baseDAO");
			Boolean ret = false;
			if ("9".equals(this.getFlag())) {// ReportController.saveReport, id is null
				try {
					ret = this.sendReport(this.getObj(), "12003");
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else if ("8".equals(this.getFlag())) {// ReportService.submitReport,id is not blank
				cn.com.generaldata.os.pojo.mvc.Report report = (cn.com.generaldata.os.pojo.mvc.Report) this.getObj();
				try {
					ret = this.sendReport(report, "12004");
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (ret) {
					cn.com.generaldata.os.pojo.Report report1 = reportService.queryReportById(report.getReportId());
					report1.setReportFlag("1");
					reportService.modifyReport(report1);
				} else {
					cn.com.generaldata.os.pojo.Report report1 = reportService.queryReportById(report.getReportId());
					report1.setReportFlag("0");
					reportService.modifyReport(report1);
				}

			} else if ("7".equals(this.getFlag())) {// ReportService.submitReport,id is null
				cn.com.generaldata.os.pojo.mvc.Report report = (cn.com.generaldata.os.pojo.mvc.Report) this.getObj();
				try {
					ret = this.sendReport(report, "12003");
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (ret) {
					cn.com.generaldata.os.pojo.Report report1 = reportService.queryReportById(report.getReportId());
					report1.setReportFlag("1");
					reportService.modifyReport(report1);
				} else {
					cn.com.generaldata.os.pojo.Report report1 = reportService.queryReportById(report.getReportId());
					report1.setReportFlag("0");
					reportService.modifyReport(report1);
				}

			} else if ("6".equals(this.getFlag())) {// AutoCallWebServiceJob
				cn.com.generaldata.os.pojo.mvc.Report report = (cn.com.generaldata.os.pojo.mvc.Report) this.getObj();
				try {
					ret = this.sendReport(report, "12005");
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else if ("5".equals(this.getFlag())) {// ReportWS.batchChangeStatus
				cn.com.generaldata.os.pojo.Report report1 = (cn.com.generaldata.os.pojo.Report) this.getObj();
				cn.com.generaldata.os.pojo.mvc.Report rep = reportService.queryMVCReportById(report1.getId());
				try {
					ret = this.sendReport(rep, "12004");
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (ret) {
					report1.setReportFlag("1");
					reportService.modifyReport(report1);
				} else {
					report1.setReportFlag("0");
					reportService.modifyReport(report1);
				}
			} else if ("4".equals(this.getFlag())) {// create user
				cn.com.generaldata.os.pojo.User user = (cn.com.generaldata.os.pojo.User) this.getObj();
				try {
					ret = this.sendReport(user, "12001");
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (ret) {
					user.setUserFlag("1");
					userDAO.update(user);
				} else {
					user.setUserFlag("0");
					userDAO.update(user);
				}
			} else if ("3".equals(this.getFlag())) {// modify user
				cn.com.generaldata.os.pojo.User user = (cn.com.generaldata.os.pojo.User) this.getObj();

				ret = this.sendReport(user, "12002");
				if (ret) {
					user.setUserFlag("1");
					userDAO.update(user);
				} else {
					user.setUserFlag("0");
					userDAO.update(user);
				}
			} else {// other conditions such as ReportService.pass,reject,saveAudit
				cn.com.generaldata.os.pojo.Report report1 = (cn.com.generaldata.os.pojo.Report) this.getObj();
				cn.com.generaldata.os.pojo.mvc.Report rep = reportService.queryMVCReportById(report1.getId());

				try {
					ret = this.sendReport(rep, "12004");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (ret) {
					report1.setReportFlag("1");
					reportService.modifyReport(report1);
				} else {
					report1.setReportFlag("0");
					reportService.modifyReport(report1);
				}
			}
		}
	}
}
